package com.qcby.springbootdemotest.controller;

import com.qcby.springbootdemotest.Util.AddressMap;
import com.qcby.springbootdemotest.Util.StringToNumber;
import com.qcby.springbootdemotest.dao.BossDao;
import com.qcby.springbootdemotest.entity.*;
import org.apache.http.client.config.RequestConfig;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.helper.HttpConnection;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.util.EntityUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Proxy;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.URL;
import java.util.List;

@Controller
@RequestMapping("/boss")
public class BossController {
    @Autowired
    private BossDao bossDao; //这里报错我们不管

    /**
     * 跳转到前端页面
     * @return
     */
    @RequestMapping("toWorm")
    public String worm(){
        return "/Worm2";
    }


    @RequestMapping("/insert")
    @ResponseBody   //加上它返回的是字符串数据，如果不加返回页面
    public void wormInsert(Boss boss) throws IOException, InterruptedException {


        //根据前端传来的值进行url的拼接
        String addressCode = AddressMap.addressMap.get(boss.getAddress());
        String sector = boss.getSector();
        String url = "https://www.zhipin.com/web/geek/job?query="+sector+"&city="+addressCode;
        System.out.println("url=" + url);

        /*
        * 模拟浏览器请求*/
        CloseableHttpClient httpClient=null;
        CloseableHttpResponse response=null;
        String result="";

        //通过地址默认配置创建一个httpClient实例
        httpClient=HttpClients.createDefault();
        //创建httpGet远程连接实例
        HttpGet httpGet=new HttpGet(url);
        //设置请求头信息，鉴权
        httpGet.setHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36");
        httpGet.setHeader("Cookie","Hm_lvt_194df3105ad7148dcf2b98a91b5e727a=1720595781; HMACCOUNT=BDC943248E54B93C; wbg=0; wd_guid=319862ed-2394-4482-aca6-945c8e01109b; historyState=state; boss_login_mode=sms; wt2=DiYe-zJR5kiM6QPnuNvlsrhLMTh_ziNmJCgK2VKIpKz-tEtxs95ddGbBnPxwn-Cmywd0srVI2teWEkzbNjp2wWA~~; zp_at=vrl1RdQJEMr8JCfKf8sGv1r8AlhyVpDOAXYo2FceYJo~; lastCity=101010100; warlockjssdkcross=%7B%22distinct_id%22%3A%22110610092%22%2C%22first_id%22%3A%221909ba26aca51d-08b6d37656c165-26001f51-1327104-1909ba26acbb03%22%2C%22props%22%3A%7B%7D%7D; __zp_seo_uuid__=3ebadab1-dbf4-48d5-aabb-b82e56b87518; __g=sem; __l=r=https%3A%2F%2Fcn.bing.com%2F&l=%2Fwww.zhipin.com%2Fsem%2F10.html%3F_ts%3D1720665887924%26sid%3Dsem%26qudao%3Dbing_pc_H120003UY5%26plan%3DBOSS-%25E5%25BF%2585%25E5%25BA%2594-%25E7%25AB%259E%25E5%2593%2581%26unit%3D%25E6%258B%2589%25E5%258B%25BE%26keyword%3D%25E6%258B%2589%25E5%258B%25BE%25E7%25BD%2591%2B%25E6%258B%259B%25E8%2581%2598%25E5%25AE%2598%25E7%25BD%2591%26msclkid%3D05a6ece02a211dd4b3c08a6f6d3cd627&s=1&g=%2Fwww.zhipin.com%2Fsem%2F10.html%3F_ts%3D1720665887924%26sid%3Dsem%26qudao%3Dbing_pc_H120003UY5%26plan%3DBOSS-%25E5%25BF%2585%25E5%25BA%2594-%25E7%25AB%259E%25E5%2593%2581%26unit%3D%25E6%258B%2589%25E5%258B%25BE%26keyword%3D%25E6%258B%2589%25E5%258B%25BE%25E7%25BD%2591%2B%25E6%258B%259B%25E8%2581%2598%25E5%25AE%2598%25E7%25BD%2591%26msclkid%3D05a6ece02a211dd4b3c08a6f6d3cd627&s=3&friend_source=0; __c=1720595780; __a=33771987.1720595781..1720595780.65.1.65.3; geek_zp_token=V1QNsnF-X92ldoVtRuzR0eLiK25TjewSg~; Hm_lpvt_194df3105ad7148dcf2b98a91b5e727a=1720679156; __zp_stoken__=771dfMjnDncK6wrPCszopDAkCBgozIDc5ICdBMiU4PzwyOTZBPjI5Ph8wIinCtMORTsKkWsODIzkpOTYyOTI7Nj86GTk6xL3CsDU2L8OJwrbDgE7CoFDDgAPCvMKzDj3CsgTDvsK6BMKEwrAHa8K5JCd%2BwrwwNzgzWsK3N8K6A8OBMMOBAMK0OsK6NzAzOiozCAtQVDMwQk1XCUJeSmFfTgVFS0wiMzM2MsKnw6LDuiwwBgkMBgUGCQwGBQkGDwoRCQYPCQIAAwoABzE2wpLCsMKpxJjDl8SBw7zElcKTU8KOwqjCusK0wq7Cm8K2wrXCiMKwwo3Cq8KOwq7Cp0LChsKpSVZfRcK6wqLCusK6wrJIb3bDgVLCs1TCuVHDgWhKwrNhYBFYCAcRMwgowo7DgA%3D%3D");

        //设置请求配置参数
        RequestConfig requestConfig=RequestConfig.custom().setConnectTimeout(350000)
                .setConnectionRequestTimeout(350000)
                .setSocketTimeout(600000)
                .build();
        //为httpGet实例设置配置
        httpGet.setConfig(requestConfig);
        //执行get请求得到返回的对象
        try {
            response = httpClient.execute(httpGet);
            //通过返回对象获取返回数据
            result = EntityUtils.toString(response.getEntity());
        }catch(IOException e){
            e.printStackTrace();
        }

        System.out.println(result);
        //
        Document document=Jsoup.parseBodyFragment(result);

        Elements content = document.getElementsByClass("job-card-wrapper");

        System.out.println("Content size: " + content.size());

        for(Element element:content){
            System.out.println("ok4");
            String salary = element.getElementsByClass("salary").text();
            String company = element.getElementsByClass("company-name").text();
            String experience = element.getElementsByClass("tag-list").text();
            String education = element.getElementsByClass("tag-list").text();

            //判断金额当中是否包含天
            if(salary.contains("天")||salary.indexOf("-")==-1){
                break; //包含天这个数据不能使用  没有“-”的数据不用
            }else {
                String s1 = StringToNumber.getValue(salary); // 获取最小的金额
                Double money = StringToNumber.StrToNum(s1) * 12;
                boss.setSalary(String.valueOf(money));
            }
            System.out.println(company+" "+salary+" "+experience+" "+education);
            boss.setCompany(company);
            boss.setExperience(experience);
            boss.setEducation(education);
            bossDao.insert(boss);
        }

    }
}